package bg.code.commons.plugin.ui;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @author 郑兴泉 956607644@qq.com
 * @data 2020/3/16
 * 描述： 错误提示弹窗
 */
public class ErrorDialog extends DialogWrapper {

    private String msg = "";
    private String title = "Error Occured, Please Retry!";

    public ErrorDialog(@Nullable Project project) {
        super(project);
        setTitle(title);
        setOKActionEnabled(false);
        init();
    }

    public ErrorDialog(@Nullable Project project, String title, String msg) {
        super(project);
        this.msg = msg;
        this.title = msg;
        setTitle(title);
        setOKActionEnabled(false);
        init();
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        if (this.msg.length() > 0) {
            JComponent centerPanel = super.createContentPane();
            JTextArea textArea = new JTextArea(this.msg, 8, 60);
            centerPanel.add(textArea);
            return centerPanel;
        }
        return null;
    }

    public static void createError(@Nullable Project project, String title, String msg) {
        new ErrorDialog(project, title, msg).show();
        return;
    }

    public static void createError(@Nullable Project project, String msg) {
        new ErrorDialog(project, "错误提示", msg).show();
        return;
    }
}
